#!/usr/bin/python
"""

    example_script.py
    [--log_file PATH]
    [--quiet]

"""

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   options        


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

from optparse import OptionParser
import sys, os
import os.path
import StringIO

# add self to search path for testing
if __name__ == '__main__':
    exe_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
    sys.path.append(os.path.abspath(os.path.join(exe_path,"..", "python_modules")))
    myname = os.path.split(sys.argv[0])[1]
    myname = os.path.splitext(myname)[0];
else:
    myname = __name__

from general_util import die_error
from custom_log import custom_log



parser = OptionParser(version="%prog 1.0")
parser.add_option("-v", "--verbose", dest = "verbose",
                  action="store_true", default=False,
                  help="Do not echo to shell but only print to log.")
parser.add_option("-L", "--log_file", dest="log_file",
                  metavar="FILE", 
                  type="string",
                  help="name and path of log file")
parser.add_option("-d", "--data_file", dest="data_file",
                  metavar="FILE", 
                  type="string",
                  help="name and path of data file")
parser.add_option("--log_parameters", dest="log_parameters",
                    action="store_true", default=False,
                    help="Print program parameters to log.")

parameters = [  
                "log_file",
                ]

mandatory_parameters = []

# parameters default to none
for parameter in parameters:
    parser.set_defaults(**{parameter: None}) 


parser.set_defaults(log_file       = os.path.join(exe_path, "meisetz.log"))
parser.set_defaults(data_file       = os.path.join(exe_path, "znf.data"))


# get help string
f =StringIO.StringIO()
parser.print_help(f)
helpstr = f.getvalue()
(options, remaining_args) = parser.parse_args()

# mandatory options
for parameter in mandatory_parameters:
    if options.__dict__[parameter] == None:
        die_error("Please specify a file in --%s.\n\n" % parameter + helpstr)



def save_option_parameters (logger):
    """
    Make a record of the program parameters so we can rerun things if necessary
    """
    programme_option_parameters = [myname + ".py"]


    store_false_options = set(i.dest for i in parser.option_list if i.action == "store_false")
    store_true_options  = set(i.dest for i in parser.option_list if i.action == "store_true")


    for option, value in options.__dict__.iteritems():
        if option in store_true_options:
            if value:
                programme_option_parameters.append("--" + option)
        elif option in store_false_options:
            if not value:
                programme_option_parameters.append("--" + option)
        else:
            if isinstance(value,str):
                programme_option_parameters.append("--" + option + " '" + str(value) + "'")

    programme_option_parameters = " ".join(programme_option_parameters)
    logger.info(programme_option_parameters + "\n")



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   imports        


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

import StringIO
import re
import operator

import general_util
from general_util import die_error
from collections import defaultdict


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Functions        


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Main logic


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

from custom_log import custom_log
logger = custom_log(options.log_file)

if options.log_parameters:
    save_option_parameters (logger)


